package com.jiutong.meipengdai.http.present;

import android.content.Context;

import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.entity.IdentifyCodeRespond;
import com.jiutong.meipengdai.entity.LoginOrRegisterRespond;
import com.jiutong.meipengdai.entity.PwdCheckRespond;
import com.jiutong.meipengdai.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipengdai.http.model.requests.UserInfoRequest;
import com.jiutong.meipengdai.http.subscribers.ProgressSubscriber;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by suxi on 2017/5/27.
 */

public class UserInfoPresent {

    private final Context mContext;
    private final RegisterAndLoginBiz mRegisterBiz;
    private final UserInfoRequest userInfoRequest;
    private final String registrationID;

    public UserInfoPresent(Context context, RegisterAndLoginBiz registerBiz) {
        this.mContext = context;
        this.mRegisterBiz = registerBiz;
        userInfoRequest = new UserInfoRequest();
        registrationID = JPushInterface.getRegistrationID(mContext);
    }

    public void register() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("phoneNo", mRegisterBiz.getPhoneNum());
        fieldsMap.put("password", mRegisterBiz.getPassword());
        fieldsMap.put("verificatCode", mRegisterBiz.getVerificatCode());

        userInfoRequest.registed(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    public void login() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<LoginOrRegisterRespond>() {
            @Override
            public void onNext(LoginOrRegisterRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("phoneNo", mRegisterBiz.getPhoneNum());
        fieldsMap.put("password", mRegisterBiz.getPassword());
        fieldsMap.put("registrationId", registrationID);


        userInfoRequest.login(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 根据旧密码更改密码
     */
    public void pwdResetByOldpwd() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("oriPwd", mRegisterBiz.getOriPwd());
        fieldsMap.put("newPwd", mRegisterBiz.getNewPwd());

        userInfoRequest.pwdResetByOldpwd(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 根据验证码更改密码
     */
    public void pwdResetByCode() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("code", mRegisterBiz.getVerificatCode());
        fieldsMap.put("newPwd", mRegisterBiz.getNewPwd());

        userInfoRequest.pwdResetByCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 获取验证码
     */
    public void getAuthCode() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<IdentifyCodeRespond>() {
            @Override
            public void onNext(IdentifyCodeRespond subjects) {
                mRegisterBiz.onVerCodeGetSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("phoneNo", mRegisterBiz.getPhoneNum());
        userInfoRequest.getAuthCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 实名认证
     */
    public void authIdCard(String invitCode, String cardType) {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("realName", mRegisterBiz.getRelName());
        fieldsMap.put("idCard", mRegisterBiz.getIdCard());
        fieldsMap.put("cardNumber", mRegisterBiz.getCardNumber());
        fieldsMap.put("cardType", cardType);
        fieldsMap.put("invitCode", invitCode);
        userInfoRequest.authIdCard(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    public void getAddress() {
        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {

            @Override
            public void onNext(BaseRespond baseRespond) {
                mRegisterBiz.onSuccess(baseRespond);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

    }


    /**
     * 换绑手机号
     */
    public void changePhoneNum() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("newTelphone", mRegisterBiz.getNewPhone());
        fieldsMap.put("authCode", mRegisterBiz.getVerificatCode());

        userInfoRequest.changePhoneNum(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 忘记密码
     */
    public void forgetPwd() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("phoneNo", mRegisterBiz.getPhoneNum());
        fieldsMap.put("code", mRegisterBiz.getVerificatCode());
        fieldsMap.put("newPwd", mRegisterBiz.getNewPwd());

        userInfoRequest.forgetPwd(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    /**
     * 邮箱验证
     */
    public void emailVerify() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("email", mRegisterBiz.getEmail());

        userInfoRequest.emailVerify(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    /**
     * 邮箱绑定
     */
    public void emailBindding() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("email", mRegisterBiz.getEmail());
        fieldsMap.put("authCode", mRegisterBiz.getVerificatCode());

        userInfoRequest.emailBind(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    /**
     * 关于我们
     */
    public void aboutUs() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
            @Override
            public void onNext(BaseRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();

        userInfoRequest.aboutUs(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 校验密码
     */
    public void checkPwd() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<PwdCheckRespond>() {
            @Override
            public void onNext(PwdCheckRespond subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("password", mRegisterBiz.getPassword());
        userInfoRequest.checkPwd(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


}
